home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / pthreadsorts / main.cp < prev    next >
Encoding:
Text File  |  2000-10-06  |  8.9 KB  |  214 lines

  1. /*
  2.     File:        main.cp
  3.  
  4.     Contains:    Main program for the PThreadSorts application which demonstrates using pthreads
  5.                         by sorting pictures.
  6.  
  7.     Written by:     Karl Groethe
  8.  
  9.     Copyright:    Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.             You may incorporate this Apple sample source code into your program(s) without
  12.             restriction. This Apple sample source code has been provided "AS IS" and the
  13.             responsibility for its operation is yours. You are not permitted to redistribute
  14.             this Apple sample source code as "Apple sample source code" after having made
  15.             changes. If you're going to re-distribute the source, we require that you make
  16.             it clear in the source that the code was descended from Apple sample source
  17.             code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                         7/00    Created
  21. */
  22. #include <Carbon/Carbon.h>
  23. #include "SortablePicture.h"
  24. #include "BubbleSortPicture.h"
  25. #include "SelectionSortPicture.h"
  26. #include "InsertionSortPicture.h"
  27. #include "ShellSortPicture.h"
  28. #include "QuickSortPicture.h"
  29. #include "HeapSortPicture.h"
  30. #include "ThreadedQuickSortPicture.h"
  31.  
  32. #define kSortItemBubbleSort    1
  33. #define kSortItemRBubbleSort    2
  34. #define kSortItemBiDirBubbleSort 3
  35. #define kSortItemSelectionSort    4
  36. #define kSortItemInsertionSort    5
  37. #define kSortItemShellSort    6
  38. #define kSortItemHeapSort    7
  39. #define kSortItemQuickSort    8
  40. #define kSortItemThreadedQuickSort2 9
  41. #define kSortItemThreadedQuickSort4 10
  42.  
  43. #define kSwapWaitUnit 1000000
  44. #define kFrameWaitUnit 5000000
  45. #define kHICommandNew        'New '
  46. #define kHICommandChangeAlgorithm 'ChAl'
  47. #define kHICommandShowHideStats 'SHst'
  48.  
  49. pascal OSStatus myCommandHandler(    EventHandlerCallRef inRef,
  50.                                                 EventRef inEvent,
  51.                                                 void* userData);
  52. pascal OSStatus myRawKeyHandler(    EventHandlerCallRef inRef,
  53.                                                 EventRef inEvent,
  54.                                                 void* userData);
  55.  
  56.                                             
  57. int main(int argc, char* argv[])
  58. {
  59.     IBNibRef         nibRef;
  60.     
  61.     static EventTypeSpec commandEvent={kEventClassCommand,kHICommandFromMenu};
  62.  
  63.     /*setup interface from nib file*/
  64.     CreateNibReference(CFSTR("main"), &nibRef);
  65.     SetMenuBarFromNib(nibRef, CFSTR("MainMenu"));
  66.     
  67.     DisposeNibReference(nibRef);
  68.     InstallApplicationEventHandler(NewEventHandlerUPP(myCommandHandler),
  69.                                 1,&commandEvent,0,NULL);
  70.                                 
  71.     #ifdef SPEED_CONTROL
  72.     static EventTypeSpec keyEvent[2]={{kEventClassKeyboard,kEventRawKeyDown},
  73.                                    {kEventClassKeyboard,kEventRawKeyRepeat}};
  74.     InstallApplicationEventHandler(NewEventHandlerUPP(myRawKeyHandler),
  75.                                 2,keyEvent,0,NULL);
  76.     #endif
  77.  
  78.     RunApplicationEventLoop();
  79.     
  80.     return 0;
  81. }
  82. #ifdef SPEED_CONTROL
  83. pascal OSStatus myRawKeyHandler(    EventHandlerCallRef inRef,
  84.                                                 EventRef inEvent,
  85.                                                 void* userData)
  86. {
  87.     /*------------------------------------------------------
  88.         Carbon Event handler for handling key presses
  89.     --------------------------------------------------------*/
  90.     char macKey;
  91.     SortablePicture* frontPicture;
  92.     WindowRef frontWindow=GetFrontWindowOfClass(kDocumentWindowClass,TRUE);
  93.     if(frontWindow){
  94.         GetWindowProperty(frontWindow,
  95.                             kAppCreator,
  96.                             SortablePicture::Class_ID,
  97.                             sizeof(SortablePicture*),
  98.                             NULL,
  99.                             &frontPicture);
  100.         GetEventParameter(inEvent,kEventParamKeyMacCharCodes,typeChar,NULL,sizeof(char),NULL,&macKey);
  101.         switch(macKey)
  102.         {
  103.             case '+'://increase the swap speed
  104.             case '=':frontPicture->SetSwapWaitTime((frontPicture->GetSwapWaitTime()).tv_nsec
  105.                                                     -kSwapWaitUnit);
  106.                     break;
  107.                     
  108.             case '-'://decrease the swap speed
  109.                      frontPicture->SetSwapWaitTime((frontPicture->GetSwapWaitTime()).tv_nsec
  110.                                                     +kSwapWaitUnit);
  111.                     break;
  112.                     
  113.             case ']'://increase frame rate
  114.                      frontPicture->SetFrameWaitTime((frontPicture->GetFrameWaitTime()).tv_nsec
  115.                                                     -kFrameWaitUnit);
  116.                     break;
  117.             case '['://decrease frame rate 
  118.                      frontPicture->SetFrameWaitTime((frontPicture->GetFrameWaitTime()).tv_nsec
  119.                                                     +kFrameWaitUnit);
  120.                     break;
  121.             default:
  122.                 return eventNotHandledErr;
  123.         }
  124.     }
  125.     return noErr;
  126. }
  127. #endif
  128. pascal OSStatus myCommandHandler(    EventHandlerCallRef inRef,
  129.                                                 EventRef inEvent,
  130.                                                 void* userData)
  131. {        
  132.     /*------------------------------------------------------
  133.       Carbon Event handler to handle menu selections
  134.     --------------------------------------------------------*/
  135.     HICommand cmd;
  136.     WindowRef    frontWindow;
  137.     static ResID    SortID=1;
  138.     //get the command
  139.     GetEventParameter(inEvent,kEventParamDirectObject,typeHICommand,NULL,sizeof(cmd),NULL,&cmd);
  140.     
  141.     switch(cmd.commandID){
  142.             case kHICommandNew:
  143.                 //create a new sortable picture
  144.                 switch(SortID){
  145.                     case kSortItemBubbleSort:
  146.                         new BubbleSortPicture(cmd.menu.menuItemIndex);
  147.                         break;
  148.                     case kSortItemRBubbleSort:
  149.                         new RBubbleSortPicture(cmd.menu.menuItemIndex);
  150.                         break;
  151.                     case kSortItemBiDirBubbleSort:
  152.                         new BiDirBubbleSortPicture(cmd.menu.menuItemIndex);
  153.                         break;
  154.                     case kSortItemSelectionSort:
  155.                         new SelectionSortPicture(cmd.menu.menuItemIndex);
  156.                         break;
  157.                     case kSortItemInsertionSort:
  158.                         new InsertionSortPicture(cmd.menu.menuItemIndex);
  159.                         break;
  160.                     case kSortItemShellSort:
  161.                         new ShellSortPicture(cmd.menu.menuItemIndex);
  162.                         break;
  163.                     case kSortItemQuickSort:
  164.                         new QuickSortPicture(cmd.menu.menuItemIndex);
  165.                         break;
  166.                     case kSortItemHeapSort:
  167.                         new HeapSortPicture(cmd.menu.menuItemIndex);
  168.                         break;
  169.                     case kSortItemThreadedQuickSort2:
  170.                         new ThreadedQuickSortPicture(cmd.menu.menuItemIndex,2);
  171.                         break;
  172.                     case kSortItemThreadedQuickSort4:
  173.                         new ThreadedQuickSortPicture(cmd.menu.menuItemIndex,4);
  174.                         break;
  175.                 }
  176.                 Str255 MenuTitle;
  177.                 //set the window title to the name of the menu selection
  178.                 GetMenuItemText(cmd.menu.menuRef,cmd.menu.menuItemIndex,MenuTitle);
  179.                 SetWTitle(GetFrontWindowOfClass(kDocumentWindowClass,TRUE),MenuTitle);
  180.                 break;
  181.             case kHICommandChangeAlgorithm:
  182.                 //change the sorting algorithm for the next picture created
  183.                 CheckMenuItem(cmd.menu.menuRef,SortID,FALSE);
  184.                 SortID=cmd.menu.menuItemIndex;
  185.                 CheckMenuItem(cmd.menu.menuRef,SortID,TRUE);
  186.                 break;
  187.             case kHICommandShowHideStats:
  188.                     //show or hide stats for the front window
  189.                     SortablePicture* frontPicture;
  190.                     frontWindow=GetFrontWindowOfClass(kDocumentWindowClass,TRUE);
  191.                     if(frontWindow){
  192.                         GetWindowProperty(  frontWindow,
  193.                                             kAppCreator,
  194.                                             SortablePicture::Class_ID,
  195.                                             sizeof(SortablePicture*),
  196.                                             NULL,
  197.                                             &frontPicture);
  198.                         if(!frontPicture->GetShowStats())
  199.                             frontPicture->SetShowStats(TRUE);
  200.                         else
  201.                             frontPicture->SetShowStats(FALSE);
  202.                     }
  203.                     break;
  204.             case kHICommandQuit:
  205.                     //close all windows before we quit
  206.                     while(frontWindow=FrontWindow()) 
  207.                         SortablePicture::DisposePictureWindow(frontWindow);
  208.                     QuitApplicationEventLoop();
  209.                     break;
  210.     }
  211.     HiliteMenu(0);
  212.     return noErr;
  213. }
  214.